<template>
    <d2-container type="card">
        <header>
            <el-form :inline="true" :model="selectFrom" class="demo-form-inline" size="mini">
                <el-form-item label="店铺名称">
                    <el-input v-model="selectFrom.shopId" placeholder="请输入店铺名称" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getIntegralExchangeData()">查询</el-button>
                    <el-button type="primary" @click="addIntegralExchangeData()">新增</el-button>
                </el-form-item>
            </el-form>
        </header>
        <footer>
            <el-table :data="tableData" border style="width: 100%" size="mini" :height="height">
                <el-table-column prop="id" label="积分兑换ID" align="center"></el-table-column>
                <el-table-column prop="shopInfo.name" label="店铺名称" align="center"></el-table-column>
                <el-table-column prop="integral" label="积分" align="center"></el-table-column>
                <el-table-column prop="price" label="兑换金额" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="updateIntegralExchangeData(scope.row)">修改</el-button>
                        <el-popconfirm title="确定删除该积分兑换么？" @confirm="deleteIntegralExchangeData(scope.row)">
                            <el-button slot="reference" size="mini" style="margin:5px" type="danger">删除</el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block" style="text-align:center">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                    :page-sizes="[5, 10, 15, 20]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </footer>
        <el-dialog :title="title" :visible.sync="integralExchangeSign" width="30%">
            <el-form :model="integralExchangeFrom" :rules="rules" ref="integralExchangeFrom" label-width="100px" class="demo-ruleForm">
                <el-form-item label="店铺名称" prop="shopId">
                    <el-input v-model="integralExchangeFrom.shopId" placeholder="请输入店铺名称"></el-input>
                </el-form-item>
                <el-form-item label="积分" prop="integral">
                    <el-input v-model="integralExchangeFrom.integral" placeholder="请输入积分"></el-input>
                </el-form-item>
                <el-form-item label="兑换金额" prop="price">
                    <el-input v-model="integralExchangeFrom.price" placeholder="请输入兑换金额"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="submit()">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </span>
        </el-dialog>
    </d2-container>
</template>
  
<script>
import { getIntegralExchange, addIntegralExchange, updateIntegralExchange, deleteIntegralExchange } from "@/api/modules/integral_exchange.js"
export default {
    name: 'IntegralExchange',
    data() {
        return {
            selectFrom: {},//搜索栏
            tableData: [],//表格数据
            page: 1,
            limit: 10,
            total: 0,
            height: window.innerHeight - 250, //表格高度
            title: "添加积分兑换",
            integralExchangeSign: false,
            isUpdate: false,//是否修改
            integralExchangeFrom: {},//用户规则表单
            rules: {
                proportion: [
                    { required: true, message: '请输入积分兑换', trigger: 'blur' },
                ]
            },//规则
        }
    },
    mounted() {
        this.getIntegralExchangeData();
        //-------------------------表格响应----------------//
        this.createHeight();
        //当页面发生变化时，表格高度固定
        window.onresize = () => {
            return (() => {
                this.$nextTick(() => {
                    this.createHeight();
                });
            })();
        };
    },
    methods: {
        //删除积分兑换
        deleteIntegralExchangeData(row) {
            deleteIntegralExchange(row).then(res => {
                this.getIntegralExchangeData();
            })
        },
        //添加积分兑换
        addIntegralExchangeData() {
            this.title = "添加积分兑换";
            this.integralExchangeSign = true;
            this.isUpdate = false;
        },
        //修改积分兑换
        updateIntegralExchangeData(row) {
            this.title = "修改积分兑换";
            this.integralExchangeSign = true;
            this.isUpdate = true;
            this.integralExchangeFrom = JSON.parse(JSON.stringify(row));
        },
        //关闭清空积分兑换表单
        closeIntegralExchangeData() {
            this.integralExchangeSign = false;
            this.cleanIntegralExchangeData();
        },
        //清空积分兑换
        cleanIntegralExchangeData() {
            this.integralExchangeFrom = {};
            if (this.$refs['integralExchangeFrom']) {
                this.$refs['integralExchangeFrom'].resetFields();
            }
        },
        submit() {
            this.$refs['integralExchangeFrom'].validate((valid) => {
                if (valid) {
                    if (this.isUpdate) {
                        updateIntegralExchange(this.integralExchangeFrom).then(res => {
                            this.closeIntegralExchangeData();
                            this.getIntegralExchangeData();
                        })
                    } else {
                        addIntegralExchange(this.integralExchangeFrom).then(res => {
                            this.closeIntegralExchangeData();
                            this.getIntegralExchangeData();
                        })
                    }
                } else {
                    return false;
                }
            });
        },
        //获取积分兑换
        getIntegralExchangeData() {
            this.selectFrom.page = this.page;
            this.selectFrom.limit = this.limit;
            getIntegralExchange({}).then(res => {
                this.tableData = res.dataList;
                this.total = res.total;
            })
        },
        handleSizeChange(val) {
            this.limit = val;
            this.getIntegralExchangeData();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getIntegralExchangeData();
        },
        //---------------自动变更表格高度------------------//
        createHeight() {
            this.height = window.innerHeight - 250;
        }
    }
}
</script>
  